<?php
/* ******************************
	Main aJax actions
   ******************************
*/
	include('../includes/classes/core.php');
	$action = $_REQUEST['act'];
	
	switch ($action) {
		case 'get_document_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("
    	                            SELECT 	overhead.id 				AS id,
											overhead.create_date		AS date,
											overhead.seller_name		AS partner,
											'ზედნადები' 				AS type
									FROM 	overhead
									WHERE 	overhead.id NOT IN (SELECT reserve.overhead_id FROM reserve) AND overhead.stream = 1
    	                           ");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );
			break;
	    case 'get_reserve_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("
    	    						SELECT  			reserve.id,
														production.code,
														production.name AS name,
														CONCAT(SUM(reserve.quantity - reserve.sold),' ', production_unit.name) as quantity,
														CONCAT(production.min_amount,' ', production_unit.name) AS minimal
									FROM  				reserve
									LEFT JOIN     		production
									ON					reserve.production_id = production.id
									LEFT JOIN     		production_unit
									ON					production.unit = production_unit.id
									WHERE				reserve.quantity - reserve.sold != 0 && reserve.quantity - reserve.sold <= production.min_amount
									GROUP BY    		reserve.production_id 	
    	    						");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					//General output 
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break;
	    case 'get_balance_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("
    	                            SELECT  `id`										AS `id`,
    	    								`partners`.`name` 							AS `name`,
											`money_credit` 								AS `credit`,
											`money_debit` 								AS `debit`,
											ROUND(`money_credit` - `money_debit`, 2) 	AS `balance`
									FROM    `partners`                 
              ");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break; 
			
		 case 'get_reserve_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("SELECT  			reserve.id,
														production.code,
														production.name AS name,
														CONCAT(SUM(reserve.quantity - reserve.sold),' ', production_unit.name) as quantity,
														CONCAT(production.min_amount,' ', production_unit.name) AS minimal
									FROM  				reserve
									LEFT JOIN     		production
									ON					reserve.production_id = production.id
									LEFT JOIN     		production_unit
									ON					production.unit = production_unit.id
									WHERE				reserve.quantity - reserve.sold != 0 && reserve.quantity - reserve.sold <= production.min_amount
									GROUP BY    		reserve.production_id 	
    	    		");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break;	   	    
	    default:
	       echo "null";
	}
?>